<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<ui:composition template="/WEB-INF/templates/template.xhtml"
	xmlns="http://www.w3.org/1999/xhtml"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:ui="http://java.sun.com/jsf/facelets"
	xmlns:a4j="http://richfaces.org/a4j"
	xmlns:rich="http://richfaces.org/rich">

	<h:outputStylesheet>
		a.no-decor>img {
		    border: none;
		}
	</h:outputStylesheet>

	<ui:define name="menu">
		<h:button value="Home" style=" width : 170px; height : 30px;" outcome = "#{loginController.home}"/>				
		<h:button value="Register" style="width : 170px; height : 30px; margin-top:10px;" outcome = "registerPage" rendered="#{loginController.user}==null"/>
		<h:form><h:commandButton value="Disconnect" style="width : 170px; height : 30px; margin-top:10px;"
		action = "#{loginController.disconnect()}"/></h:form>		
	</ui:define>
	
	<ui:define name="content">		
		<h:outputLabel style="color:#8080FF;font-style:normal;font-size:xx-large;text-decoration:blink;
		font-weight:bold;" value="Welcome Administrator #{loginController.user.firstName}!!"/>
		 <rich:tabPanel switchType="client">
        	<rich:tab header="Create">    
		        <div id="stylized" class="myform">
					<h:form>
						<h:panelGrid columns="3">
							<h:outputLabel for="firstName" value="First Name" />
							<h:inputText id="firstName" value="#{userBean.user.firstName}"
								required="true" />
							<h:message for="firstName" />
							<h:outputLabel for="lastName" value="Last Name" />
							<h:inputText id="lastName" value="#{userBean.user.lastName}"
								required="true" />
							<h:message for="lastName" />
							<h:outputLabel for="login" value="Login" />
							<h:inputText id="login" value="#{userBean.user.login}"
								required="true" />
							<h:message for="login" />
							<h:outputLabel for="password" value="Password" />
							<h:inputSecret id="password" value="#{userBean.user.password}"
								required="true" />
							<h:message for="password" />
							<h:outputLabel for="role" value="Role" />
							<rich:select id="role" value="#{userBean.user.role}" required="true">
								<f:selectItem itemLabel="-Select-" noSelectionOption="true" />
								<f:selectItem itemValue="Administrator" itemLabel="Administrator" />
								<f:selectItem itemValue="Simple User" itemLabel="Simple User" />
							</rich:select>
							<h:message for="role" />
							<h:commandButton action="#{register.createUser(userBean.user)}"
								value="Add user"
								style="width : 150px;; margin-top : 20px; margin-left : 20px; height : 30px;" />
						</h:panelGrid>
					</h:form>
				</div>    
        	</rich:tab>
        	<rich:tab header="Edit and Delete">
		    <a4j:status onstart="#{rich:component('statPane')}.show()"
		        onstop="#{rich:component('statPane')}.hide()" />
		    <h:form id="form">
		        <rich:dataTable value="#{adminController.listUsers}" var="user" iterationStatusVar="it" id="table" rows="15">
		            <rich:column>
		                <f:facet name="header">#</f:facet>
		                #{it.index}
		            </rich:column>
		            <rich:column>
		                <f:facet name="header">First Name</f:facet>
		                <h:outputText value="#{user.firstName}" />
		            </rich:column>
		            <rich:column>
		                <f:facet name="header">Last Name</f:facet>
		                <h:outputText value="#{user.lastName}" />
		            </rich:column>
		            <rich:column>
		                <f:facet name="header">Login</f:facet>
		                <h:outputText value="#{user.login}" />
		            </rich:column>
		            <rich:column>
		                <f:facet name="header">Password</f:facet>
		                <h:outputText value="#{user.password}" />
		            </rich:column>
		            <rich:column>
		                <f:facet name="header">Role</f:facet>
		                <h:outputText value="#{user.role}" />
		            </rich:column>
		            <rich:column>
		            
		                <a4j:commandLink styleClass="no-decor" execute="@this" render="@none" 
		                oncomplete="#{rich:component('confirmPane')}.show()">
		                    <h:graphicImage library="images" name="delete.gif" alt="delete" />
		                    <a4j:param value="#{it.index}" assignTo="#{adminController.currentUserIndex}" />
		                </a4j:commandLink>
		                		               
		                <a4j:commandLink styleClass="no-decor" render="editGrid"
		                    execute="@this" oncomplete="#{rich:component('editPane')}.show()" action="#{adminController.setEdited()}">
		                    <h:graphicImage library="images" name="edit.gif" alt="edit"/>
		                    <a4j:param value="#{it.index}" assignTo="#{adminController.currentUserIndex}" />		                    
		                </a4j:commandLink>
		                
		            </rich:column>
		            <f:facet name="footer">
		                <rich:dataScroller page="#{adminController.page}" />
		            </f:facet>
		        </rich:dataTable>
		 
		        <a4j:jsFunction name="remove" action="#{adminController.remove}"
		            render="table" execute="@this"
		            oncomplete="#{rich:component('confirmPane')}.hide();" />
		 
		    <rich:popupPanel id="statPane" autosized="true">
		        <h:graphicImage library="images" name="ai.gif" alt="ai" />
		        Please wait...
		    </rich:popupPanel>
		 
		    <rich:popupPanel id="confirmPane" autosized="true"> 
		        Are you sure you want to delete the row?
		        <a4j:commandButton value="Cancel"
		            onclick="#{rich:component('confirmPane')}.hide(); return false;" />
		        <a4j:commandButton value="Delete" onclick="remove(); return false;" />
		    </rich:popupPanel>
		 
		    <rich:popupPanel header="Edit User with login: " id="editPane" domElementAttachment="parent" width="300" height="220">
		            <h:panelGrid columns="3" id="editGrid">
							<h:outputLabel for="firstName" value="First Name" />
							<h:inputText id="firstName" value="#{adminController.editedUser.firstName}"	required="true" />
							<h:message for="firstName" />
							<h:outputLabel for="lastName" value="Last Name" />
							<h:inputText id="lastName" value="#{adminController.editedUser.lastName}"	required="true" />
							<h:message for="lastName" />							
							<h:outputLabel for="login" value="Login" />
							<h:inputText id="login" value="#{adminController.editedUser.login}"	required="true" />
							<h:message for="login" />
							<h:outputLabel for="password" value="Password" />
							<h:inputSecret id="password" value="#{adminController.editedUser.password}"	required="true" />
							<h:message for="password" />
							<h:outputLabel for="role" value="Role" />
							<rich:select id="role" value="#{adminController.editedUser.role}" required="true">
								<f:selectItem itemLabel="-Select-" noSelectionOption="true" />
								<f:selectItem itemValue="Administrator" itemLabel="Administrator" />
								<f:selectItem itemValue="Simple User" itemLabel="Simple User" />
							</rich:select>
							<h:message for="role" />							
						</h:panelGrid>
		            <a4j:commandButton value="Store" action="#{adminController.update()}" render="table" execute="editPane"
		                oncomplete="if (#{facesContext.maximumSeverity==null}) {#{rich:component('editPane')}.hide();}" />
		            <a4j:commandButton value="Cancel"  onclick="#{rich:component('editPane')}.hide(); return false;" />
		    </rich:popupPanel>
		    </h:form>
        	</rich:tab>
        	        	
    </rich:tabPanel>
	</ui:define>
	
</ui:composition>